package com.agadmin.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author wdx
 * @date 2022/4/10
 * 根据filename获取真实的bean
 * 通过反射调用bean的handler方法
 *
 * */

@Component
public class ServiceApi {
    @Autowired
    private ApplicationContext applicationContext;

    public Object handler(Class<?> bean, Object req)throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Object beanName =applicationContext.getBean(bean);
        Method handler = beanName.getClass().getDeclaredMethod("handler", Object.class);
        return handler.invoke(beanName,req);
    }

}
